<template>
  <a-card :bordered='false'>
    <!-- 查询区域 -->
    <div class='table-page-search-wrapper'>
      <a-form layout='inline' @keyup.enter.native='searchQuery'>
        <a-row :gutter='24'>
          <a-col :lg='7' :md='8' :sm='24' :xl='6'>
            <a-form-item label='销售出库单号'>
              <a-input v-model='queryParam.serial' placeholder='请输入销售出库单号'></a-input>
            </a-form-item>
          </a-col>
          <a-col :lg='7' :md='8' :sm='24' :xl='6'>
            <a-form-item label='客户'>
              <j-search-select-tag v-model='queryParam.customId' dict='hf_custom,name,id' placeholder='请选择客户' />
            </a-form-item>
          </a-col>
          <a-col :md='6' :sm='6'>
            <a-form-item label='客户订单号'>
              <a-input v-model='queryParam.customOrderSerial' />
            </a-form-item>
          </a-col>
          <a-col :md='6' :sm='6'>
            <a-form-item label='关联销售订单号'>
              <a-input v-model='queryParam.sourceSerial' type='list' />
            </a-form-item>
          </a-col>
          <a-col :md='6' :sm='6'>
            <a-form-item label='单据日期'>
              <j-date v-model='queryParam.documentDate' style='width: 100%' type='list' />
            </a-form-item>
          </a-col>
          <a-col :md='6' :sm='6'>
            <a-form-item label='日期区间'>
              <a-range-picker v-model='queryParam.dateInterval' type='list' @change='handleDateInterval' />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class='table-operator'>
      <a-button icon='search' type='primary' @click='searchQuery'>查询</a-button>
      <a-button icon='reload' type='primary' @click='searchReset'>重置</a-button>
      <a-button icon='download' type='primary' @click="handleExportXls('销售出库审核单')">导出</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class='ant-alert ant-alert-info' style='margin-bottom: 16px;'>
        <i class='anticon anticon-info-circle ant-alert-icon'></i> 已选择
        <a style='font-weight: 600'>{{ selectedRowKeys.length }}</a
        >项
        <a style='margin-left: 24px' @click='onClearSelected'>清空</a>
      </div>

      <a-table
        ref='table'
        :columns='columns'
        :customRow='clickThenSelect'
        :dataSource='dataSource'
        :loading='loading'
        :pagination='ipagination'
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange, type: 'radio' }"
        :scroll='{ x: true }'
        bordered
        class='j-table-force-nowrap'
        rowKey='id'
        size='middle'
        @change='handleTableChange'
      >
        <template slot='isClosed' slot-scope='text'>
          <a-tag :color="text === '未关闭' ? 'red' : 'green'">{{ text }}</a-tag>
        </template>
        <template slot='status' slot-scope='text'>
          <a-tag :color="text === '未审核' ? 'red' : 'green'">{{ text }}</a-tag>
        </template>

        <span slot='action' slot-scope='text, record'>
          <a v-if='record.status === 0' @click='handleEdit(record)'>编辑</a>
          <a-divider v-if='record.status === 0' type='vertical' />
          <a v-if='record.status === 0' @click='handleDelete(record.id)'>删除</a>
          <a-divider v-if='record.status === 0' type='vertical' />
          <a v-if='record.status === 1' @click='handleUndoCheck(record)'>反审核</a>
          <a-divider v-if='record.status === 1' type='vertical' />
          <a v-if='record.status === 0' @click='handleCheck(record)'>审核</a>
          <a-divider v-if='record.status === 0' type='vertical' />
          <a v-if='record.isClosed === 0' @click='handleClose(record)'>关闭</a>
          <a v-if='record.isClosed === 1' @click='handleUndoClose(record)'>反关闭</a>
        </span>
      </a-table>
    </div>

    <a-tabs defaultActiveKey='1'>
      <a-tab-pane key='1' tab='明细'>
        <HfSaleoutDetailList ref='DetailList' :mainId='selectedMainId' />
      </a-tab-pane>
      <a-tab-pane key='2' tab='标签'>
        <HfSaleOutDetailLabelList ref='labelListRef' :mainId='selectedMainId' />
      </a-tab-pane>
    </a-tabs>

    <hfSaleout-modal ref='modalForm' @ok='modalFormOk'></hfSaleout-modal>
  </a-card>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import HfSaleoutModal from './modules/HfSaleoutModal'
import HfSaleoutDetailList from './HfSaleOutDetailList.vue'
import '@/assets/less/TableExpand.less'
import { getAction } from '@api/manage'
import HfSaleOutDetailLabelList from '@views/sale/HfSaleOutDetailLabelList.vue'
import dayjs from 'dayjs'

export default {
  name: 'HfSaleoutList',
  mixins: [JeecgListMixin],
  components: {
    HfSaleOutDetailLabelList,
    HfSaleoutDetailList,
    HfSaleoutModal
  },
  data() {
    return {
      description: '销售出库管理页面',
      dateInterval: null,
      /* 排序参数 */
      isorter: {
        column: 'documentDate',
        order: 'desc'
      },
      // 表头
      columns: [
        {
          title: '出库单号',
          align: 'center',
          dataIndex: 'serial'
        },
        {
          title: '单据日期',
          align: 'center',
          dataIndex: 'documentDate'
        },
        {
          title: '交货日期',
          align: 'center',
          dataIndex: 'deliveryDate',
          customRender: text => {
            return dayjs(text).format('YYYY-MM-DD')
          }
        },
        {
          title: '客户',
          align: 'center',
          dataIndex: 'customId_dictText'
        },
        {
          title: '客户订单号',
          align: 'center',
          dataIndex: 'supplierOderSerial'
        },
        {
          title: '关联销售订单号',
          align: 'center',
          dataIndex: 'sourceSerial'
        },
        {
          title: '红蓝单',
          align: 'center',
          dataIndex: 'isRed_dictText'
        },
        {
          title: '制单',
          align: 'center',
          dataIndex: 'operator'
        },
        {
          title: '审核员',
          align: 'center',
          dataIndex: 'checker'
        },
        {
          title: '审核时间',
          align: 'center',
          dataIndex: 'checkTime'
        },
        {
          title: '审核状态',
          align: 'center',
          dataIndex: 'status_dictText',
          scopedSlots: { customRender: 'status' }
        },
        {
          title: '关闭状态',
          align: 'center',
          dataIndex: 'isClosed_dictText',
          scopedSlots: { customRender: 'isClosed' }
        },
        {
          title: '备注',
          align: 'center',
          dataIndex: 'remark'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/sale/hfSaleOut/checkList',
        exportXlsUrl: '/sale/hfSaleOut/exportXls',
        importExcelUrl: 'sale/hfSaleout/importExcel',
        check: '/sale/hfSaleOut/check',
        undoCheck: '/sale/hfSaleOut/undoCheck',
        close: '/sale/hfSaleOut/close',
        undoClose: '/sale/hfSaleOut/undoClose'
      },
      dictOptions: {
        isRed: []
      },
      /* 分页参数 */
      ipagination: {
        current: 1,
        pageSize: 5,
        pageSizeOptions: ['5', '10', '50'],
        showTotal: (total, range) => {
          return range[0] + '-' + range[1] + ' 共' + total + '条'
        },
        showQuickJumper: true,
        showSizeChanger: true,
        total: 0
      },
      selectedMainId: '',
      superFieldList: []
    }
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  methods: {
    handleDateInterval(value) {
      this.queryParam['documentDate_begin'] = value[0].format('YYYY-MM-DD')
      this.queryParam['documentDate_end'] = value[1].format('YYYY-MM-DD')
      console.log(this.endDate)
    },
    clickThenSelect(record) {
      return {
        on: {
          click: () => {
            this.onSelectChange(record.id.split(','), [record])
          }
        }
      }
    },
    onClearSelected() {
      this.selectedRowKeys = []
      this.selectionRows = []
      this.selectedMainId = ''
    },
    onSelectChange(selectedRowKeys, selectionRows) {
      this.selectedMainId = selectedRowKeys[0]
      this.selectedRowKeys = selectedRowKeys
      this.selectionRows = selectionRows
      this.$refs.DetailList.isChecked = true
    },
    loadData(arg) {
      if (!this.url.list) {
        this.$message.error('请设置url.list属性!')
        return
      }
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1
      }
      var params = this.getQueryParams() //查询条件
      this.loading = true

      if (this.beginDate) {
        params.beginDate = this.beginDate
      }
      if (this.endDate) {
        params.endDate = this.endDate
      }
      if (this.materialCode) {
        params.materialCode = this.materialCode
      }
      if (this.materialName) {
        params.materialName = this.materialName
      }

      getAction(this.url.list, params)
        .then(res => {
          if (res.success) {
            //update-begin---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
            this.dataSource = res.result.records || res.result
            if (res.result.total) {
              this.ipagination.total = res.result.total
            } else {
              this.ipagination.total = 0
            }
            //update-end---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.loading = false
        })
    },
    searchReset() {
      this.queryParam = {}
      if (this.$refs.superQueryModal !== undefined) {
        this.$refs.superQueryModal.handleReset()
      }
      this.loadData(1)
    },
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
